package com.imooc.controller;

import com.imooc.dataobject.SellerInfo;
import com.imooc.form.SellerForm;
import com.imooc.service.SellerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RequestMapping("/seller")
@Controller
@Slf4j
public class SellerUserController {

    @Autowired
    private SellerService sellerService;

    @RequestMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("/seller/login");
    }

    @PostMapping("/login")
    public String login(@Validated SellerForm sellerForm,
                        BindingResult bindingResult) {
        SellerInfo sellerInfo = sellerService.findOne(sellerForm.getSellerId());
        if (!sellerForm.getPassword().equals(sellerInfo.getPassword())) {
            log.info("【卖家用户登录】 密码不正确, password={}", sellerForm.getPassword());
            return "redirect:/seller/index";
        }
        return "redirect:/seller/order/list";
    }

    /**
     * 退出其实就是清空session
     *
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "redirect:/seller/index";
    }

}
